<?php
defined("_ALLOW") or die ("Access denied");
?>
<?php
class mkuser {
	var $id;
	var $email;
	var $password;
	var $hoten;
	var $dienthoai;
	var $diachi;
	var $congty;
	var $website;
	var $vip = 0;
	var $sid;
	var $registertime;
	var $active;
	
	function Fill ($strwhere="") {
		global $csdl;
		$strsql = "SELECT * FROM mk_users";
		if ($strwhere!="") {
			$strsql .= " WHERE " . $strwhere;
		}
		$strsql .= " ORDER BY email";
		$rowsdb = $csdl->Truyvan ($strsql);
		$i=0;
		$this->nums=0;
		if ($rowsdb==false) return null;
		while ($rowdb = mysql_fetch_array($rowsdb,MYSQL_ASSOC)) {
			$rowarray[$i] = $this->Khoi_tao($rowdb);
			$i++;
		}
		if ($i==0) return null;
		$this->nums = $i;
		return $rowarray;
	}
	function Doc ($email) {
		$rowarray = $this->Fill ("email='$email'");
		if ($rowarray==null)
			return null;
		else
			return $rowarray[0];
	}
	function DocId ($id) {
		$rowarray = $this->Fill ("id=$id");
		if ($rowarray==null)
			return null;
		else
			return $rowarray[0];
	}
	function Xoa () {
		//checksupper() or die ("Access denied");
		global $csdl;
		$csdl->Xoa("mk_users","email='$this->email'");
		return 1;
	}
	function DocForm () {
		$this->id = $_REQUEST['id'];
		$this->email = $_REQUEST['email'];
		$this->password = $_REQUEST['password'];
		$this->hoten = $_REQUEST['hoten'];
		$this->dienthoai = $_REQUEST['dienthoai'];
		$this->diachi = $_REQUEST['diachi'];
		$this->congty = $_REQUEST['congty'];
		$this->website = $_REQUEST['website'];
		$this->vip = $_REQUEST['vip'];
		/*if($_REQUEST['vip'] == 1)
			$this->vip = 1;
		else
			$this->vip =0;*/
		//$this->active = $_REQUEST['active'];
	}
	function Khoi_tao ($rowdb) {
		$kq = new mkuser;
		$kq->id = $rowdb['id'];
		$kq->email = $rowdb['email'];
		$kq->password = $rowdb['password'];
		$kq->hoten = $rowdb['hoten'];
		$kq->dienthoai = $rowdb['dienthoai'];
		$kq->diachi = $rowdb['diachi'];
		$kq->congty = $rowdb['congty'];
		$kq->website = $rowdb['website'];
		$kq->vip = $rowdb['vip'];
		$kq->active = $rowdb['active'];
		$kq->sid = $rowdb['sid'];
		$kq->registertime = $rowdb['registertime'];
		return $kq;
	}
	function GetMaxId () {
		$strwhere = "id>=all";
		$strwhere .= " (SELECT id FROM mk_users)";
		$rowarray = $this->Fill ($strwhere, 1, 0);
		if ($rowarray==null) return 0;
		return $rowarray[0]->id;
	}
	function Ghi () {
//		checkadmin() or die ("Access denied");
		global $csdl;
		$strsql  = "UPDATE `mk_users` SET ";
		$strsql .= "`hoten` = '$this->hoten' ";
		if ($this->password != "")
			$strsql .= ", `password` = md5('$this->password') ";
		$strsql .= ", `dienthoai` = '$this->dienthoai' ";
		$strsql .= ", `diachi` = '$this->diachi' ";
		$strsql .= ", `congty` = '$this->congty'";
		$strsql .= ", `website` = '$this->website'";
		$strsql .= ", `vip` = '$this->vip'";
//		$strsql .= ", `active` = $this->active";
		$strsql .= " WHERE email='$this->email'";
//		echo $strsql;
		$csdl->Ghi($strsql);
	}
	function Register () {
		$newid = $this->GetMaxId()+1;
		global $csdl;
		$strsql  = "INSERT INTO `mk_users`(`id`,`email`, `password`, `hoten`, `dienthoai`, `diachi`, `congty`, `website`, `sid`, `registertime`, `active`, `vip`) ";
		$strsql .= "VALUES ($newid, '$this->email', md5('$this->password'), '$this->hoten', '$this->dienthoai', '$this->diachi', '$this->congty', '$this->website', '$this->sid', SYSDATE(), 1, $this->vip)";
		$csdl->Ghi($strsql);
	}
	function ResetPassword () {
		global $csdl;
		$strsql  = "UPDATE `mk_users` SET ";
		$strsql .= " `password` = md5('$this->password') ";
		$strsql .= " WHERE email='$this->email'";
		$csdl->Ghi($strsql);
	}
	function Active () {
		global $csdl;
		$strsql  = "UPDATE `mk_users` ";
		$strsql .= " SET `active` = 1";
		$strsql .= " WHERE `email` ='" . $this->email . "'";
		$csdl->Ghi($strsql);
	}
	function ClearRegister () {
		global $csdl;
		$csdl->Xoa("mk_users", "active=0 AND ADDDATE(registertime,INTERVAL 1 DAY)>=SYSDATE()");
	}
};
global $objmkuser; $objmkuser = new mkuser;
?>